<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="脚本名称" prop="scriptName">
        <el-input
          v-model="queryParams.scriptName"
          placeholder="请输入脚本名称"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主机名称" prop="hostname">
        <el-input
          v-model="queryParams.hostname"
          placeholder="请输入主机名称"
          clearable
          style="width: 240px;"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行状态" prop="flag">
        <el-select style="width: 240px" v-model="queryParams.flag" placeholder="执行状态" clearable>
          <el-option key="dict.value" label="所有" value="" />
          <el-option key="0" label="执行失败" value="0" />
          <el-option key="1" label="执行成功" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="rangedate"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh"  @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除日志</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="logList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="number" width="60" />
      <el-table-column label="执行脚本名称" align="center" prop="scriptName" />
      <el-table-column label="执行主机名称" align="center" prop="hostname" />
      <el-table-column label="是否成功" align="center" prop="flag">
        <template #default="{ row }">
          <span>{{ row.flag == 1 ? "执行成功" : "执行失败" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            link type="primary" 
            icon="View"
            @click="handleView(row)"
            >查看日志</el-button
          >
          <el-button
            type="text"
            icon="Delete"
            @click="handleDelete(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!--查看对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      width="800px"
      append-to-body
    >
      <!-- <Shditor ref="editors" :content="logContent" :readOnly="readOnly"></Shditor> -->
      <ShEditor v-model="logContent" :readOnly="readOnly" />

      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Log">
import { list, del } from "@/api/script/log";
import ShEditor from "@/components/common/ShEditor.vue";

import { ref, reactive, onMounted } from "vue";
const { proxy } = getCurrentInstance();
// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 岗位表格数据
const logList = ref([]);
// 弹出层标题
const title = ref("");
const logContent = ref("");
// 是否显示弹出层
const open = ref(false);

const readOnly = ref(true);
const rangedate = ref([]);
// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  hostname: undefined,
  scriptName: undefined,
  flag: undefined,
  params: {},
});

const getList = () => {
  loading.value = true;
  queryParams.params["startTime"] = rangedate.value[0];
  queryParams.params["endTime"] = rangedate.value[1];
  list(queryParams).then((response) => {
    logList.value = response.rows.map((item, index) => {
      item.number = index + 1;
      return item;
    });
    total.value = response.total;
    loading.value = false;
  });
};

const cancel = () => {
  open.value = false;
  getList();
};

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryParams.hostname = undefined;
  queryParams.scriptName = undefined;
  queryParams.flag = undefined;
  rangedate.value = [];
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id).join(",");
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

const handleView = (row) => {
  logContent.value = row.content;
  open.value = true;
  title.value = "查看日志";
};

const submitForm = () => {
  open.value = false;
  getList();
};

const handleDelete = (row) => {
  const id = row.id || ids.value;
  debugger;
  proxy.$confirm('是否确认删除编号为"' + id + '"的数据项？')
    .then(() => {
      return del(id);
    })
    .then(() => {
      getList();
      proxy.$message.success("删除成功");
    })
    .catch(() => {});
};

getList();
</script>